package com.app.service.impl;

import com.app.mapper.AdminUserMapper;
import com.app.mapper.DepartmentMapper;
import com.app.model.AdminUserModel;
import com.app.model.DepartmentModel;
import com.app.service.AdminUserService;
import com.app.service.DepartmentService;
import com.app.vo.AdminUserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public AdminUserModel getUserByUserName(String account, String pwd) {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("account", account);
        queryWrapper.select().eq("pwd", pwd);
        List<AdminUserModel> userList = adminUserMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public AdminUserModel getUserById(Integer userId) {
        return adminUserMapper.selectById(userId);
    }

    @Override
    public void update(AdminUserModel user) {
        adminUserMapper.updateById(user);
    }

    @Override
    public void insertAdmin(AdminUserModel adminUserModel) {
        adminUserMapper.insert(adminUserModel);
    }

    @Override
    public void updateAdmin(AdminUserModel adminUserModel) {
        adminUserMapper.updateById(adminUserModel);
    }

    @Override
    public void deleteAdmin(Integer id) {
        adminUserMapper.deleteById(id);
    }

    @Override
    public AdminUserVo selectAdmin(Integer page, Integer limit, String account) {
        List<DepartmentModel> departmentModels = departmentMapper.get();
        page = (page - 1) * limit;
        List<AdminUserModel> adminUserModels = adminUserMapper.selectAdmin(page, limit, account);
        for (int i = 0; i < adminUserModels.size(); i++) {
            for (int i1 = 0; i1 < departmentModels.size(); i1++) {
                if (adminUserModels.get(i).getDepartmentId() == departmentModels.get(i1).getId()) {
                    adminUserModels.get(i).setDepartmentText(departmentModels.get(i1).getUnitName());
                }
            }
        }
        for (int i = 0; i < adminUserModels.size(); i++) {
            if (adminUserModels.get(i).getDepartmentId() == 0) {
                adminUserModels.get(i).setIdentity("超级管理员");
            } else {
                adminUserModels.get(i).setIdentity("单位部门管理员");
            }
        }
        AdminUserVo vo = new AdminUserVo();
        vo.setList(adminUserModels);
        vo.setCount(adminUserModels.size());
        return vo;
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        AdminUserModel model = new AdminUserModel();
        model.setStatus(status);
        model.setId(id);
        adminUserMapper.updateById(model);
    }
}
